var layer;
var $;
var element;
var form;
var laydate;
var table;
layui.use(['element','layer','laydate','form','table'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    form = layui.form;
    laydate = layui.laydate;
    table = layui.table;
    load();
    selIndexUserTab();
})
/**
 *  点击事件  刷新  返回
 * */
function load(){
    $(".tit_btn").bind('click',function(){
        location.reload();
    })
    $(".tit_btn2").bind('click',function(){
        window.history.go(-1);  //返回上一页
    })
    $("#indexBtn").bind('click',function(){
        var obj = $("#selIndex").serialize();
        selIndexUserTab(obj)
    })
}
/**
 *  封号用户列表
 */
function selIndexUserTab(vals){
    $.ajax({
        url:"/personnel/selClosureUserTable",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(item) {
            userTab(item);
        }
    })
}
function userTab(item){
    table.render({
        elem:"#userListTab",//容器
        title:"封号用户数据列表",
        limit:10,//分页显示条数
        width:1223,
        data:item,
        toolbar:true,
        page:{//自定义分页
            layout:['count','prev','page','next','limit','skip','refresh'],
            limits:[3,6,9],//分页选项
            first:"首页",
            last:"尾页",
        },
        cols:[[//定义列
            {type:'checkbox',align:'center',width:60},//复选框靠左
            {title:'序号',align:'center',templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field:"userId",title:"用户ID",align:'center',width:120},
            {field:"startDate",title:"封停时间",align:'center',width:190},
            {field:"endDate",title:"解封时间",align:'center',width:190},
            {field:"reason",title:"封停原因",align:'center',width:200},
            {field:"days",title:"封停天数",align:'center',width:120,templet:function(data){
                if(data.days==0){
                    return "已解封";
                }else{
                    return data.days+"天";
                }
             }},
            {field:"",title:"用户",align:'center',width:120,templet:function(data){
                    if(data.type==2){
                        return "前端用户";
                    }else if(data.type==3){
                        return "管理端人员";
                    }
                }},
            {field:"",title:"操作",align:'center',width:140,templet:function(data){
                    return "<a style='color:#009688;' onclick='checkswitchon(\""+data.userId+"\",\""+data.reason+"\",\""+data.id+"\",\""+data.days+"\")'>解封</a> " +
                        "<a style='color:#009688;' onclick='delUser(\""+data.userId+"\",\""+data.id+"\",\""+data.days+"\")'>删除</a>" ;
                }}
        ]]
    });
}
/**
 *  删除用户
 */
function delUser(userId,id,days){
    if(days!=0){
        layer.msg("该账号目前处于封禁状态，无法删除");
    }else{
        $.ajax({
            url:"/personnel/selUserInfo",
            type:"post",
            dataType:"json",
            data:{"userId":userId},
            success:function(item) {
                layer.confirm("确定删除`"+item.account+"`账户的封停日志？",{btn: ['确定', '取消'],title:"提示"}, function(){
                    $.ajax({
                        url:"/personnel/delBeforeStatus",
                        type:"post",
                        dataType:"json",
                        data:{"id":id},
                        success:function(data){
                            if(data){
                                layer.msg("删除成功");
                                selIndexUserTab();
                            }else{
                                layer.msg("删除失败");
                                selIndexUserTab();
                            }
                        }
                    })
                });
            }
        })
    }
}
/**
 *  管理启用开关
 */
function checkswitchon(userId,reason,id,days){
    if(days==0){
        layer.msg("该账户已被解封")
    }else{
        layer.open({
            type:2,//弹出层的类型
            title:"解封用户",
            area:['550px','400px'],//弹出层大小
            shade:0,//设置遮罩
            offset:'auto',//设置遮罩的位置
            maxmin:true,
            //弹出层内容
            content :"/personnel/PerUserList-layer-checkswitchon.html",
            success:function(layero,index){
                $.ajax({
                    url:"/personnel/selUserInfo",
                    type:"post",
                    dataType:"json",
                    data:{"userId":userId},
                    success:function(item) {
                        //查找当前引入的iframe的内容
                        var obj = layero.find('iframe').contents();
                        obj.find("input[name=userId]").val(userId);
                        obj.find("input[name=reason]").val(reason);
                        obj.find("input[name=account]").val(item.account);
                        obj.find("input[name=nickname]").val(item.nickname);
                        obj.find("input[name=id]").val(id);
                    }
                })
            },
            btn:['确定','取消'],
            yes:function(index,layero){
                var body = layero.find('iframe').contents();
                var form = $(body).find('form').serialize();
                updClosure(form);
                layer.close(layer.index);
            },btn2:function(index,layero){

            }
        })
    }
}
function updClosure(vals){
    $.ajax({
        url:"/personnel/updClosure",
        type:"post",
        dataType:"json",
        data:vals,
        success:function(data){
            if(data){
                layer.msg("解封成功");
                selIndexUserTab();
            }else{
                layer.msg("解封失败");
                selIndexUserTab();
            }
        }
    })
}